package com.yunhui.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;


/**
 * 生成token的工具类
 * @author Administrator
 *
 */
public class TokenUtils {
public static String generateToken(String companyCode){
		
		if(companyCode == null || companyCode.equals("")){
			return null;
		}
		
		MessageDigest md = null;
		try {
			md = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}  
	    md.update(companyCode.getBytes());
	    byte b[] = md.digest();
	    int i;
	    StringBuffer buf = new StringBuffer("");
	    for (int offset = 0; offset < b.length; offset++) {
		      i = b[offset];
		      if(i<0) i+= 256;
		      if(i<16)
		           buf.append("0");
		           buf.append(Integer.toHexString(i));
	    }
		String haxString = buf.toString();
		String random = UUID.randomUUID().toString();
		random = random.replaceAll("-", "");
		String result = haxString+random;
		return result.toUpperCase();
	}
	
	public static String generateToken16(String companyCode){
		
		if(companyCode == null || companyCode.equals("")){
			return null;
		}
		
		MessageDigest md = null;
		try {
			md = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}  
	    md.update(companyCode.getBytes());
	    byte b[] = md.digest();
	    int i;
	    StringBuffer buf = new StringBuffer("");
	    for (int offset = 0; offset < b.length; offset++) {
		      i = b[offset];
		      if(i<0) i+= 256;
		      if(i<16)
		           buf.append("0");
		           buf.append(Integer.toHexString(i));
	    }
		String haxString = buf.toString().substring(8, 24);
		String random = UUID.randomUUID().toString();
		random = random.replaceAll("-", "").substring(16);
		String result = haxString+random;
		return result.toUpperCase();
	}
}
